package com.tongji.diy.view
{
	import com.tongji.diy.event.AddHoleEvent;
	import com.tongji.diy.event.AddNewFurnitureEvent;
	import com.tongji.diy.event.AddRoomEvent;
	import com.tongji.diy.event.AddShapeEvent;
	import com.tongji.diy.event.BuildRoomEvent;
	import com.tongji.diy.event.ChangeShapeEvent;
	import com.tongji.diy.event.FurnitureFinalStateEvent;
	import com.tongji.diy.event.FurnitureLocationAndRotation;
	import com.tongji.diy.event.FurnitureManualRotationEvent;
	import com.tongji.diy.event.FurnitureOverlapEvent;
	import com.tongji.diy.event.FurnitureRotationEvent;
	import com.tongji.diy.event.LockFurnitureEvent;
	import com.tongji.diy.event.NewDoorEvent;
	import com.tongji.diy.event.GetDoors;
	import com.tongji.diy.event.NewFurnitureEvent;
	import com.tongji.diy.event.RoomRotationEvent;
	import com.tongji.diy.event.SetRoomPositionEvent;
	import com.tongji.diy.event.SetRoomRotationEvent;
	import com.tongji.diy.event.ShowNewShapeEvent;
	import com.tongji.diy.event.TestBoundBoxEvent;
	import com.tongji.diy.event.TestEnterClickEvent;
	import com.tongji.diy.event.TestShowLayOutEvent;
	import com.tongji.diy.event.UpdateRoomBorderEvent;
	import com.tongji.diy.event.UpdateRoomFloorEvent;
	import com.tongji.diy.event.UpdateRoomVertexEvent;
	import com.tongji.diy.event.UploadHoleEvent;
	import com.tongji.diy.event.UploadRoomBorderEvent;
	import com.tongji.diy.event.UploadRoomPositionEvent;
	import com.tongji.diy.event.UploadRoomRotationEvent;
	import com.tongji.diy.event.UploadRoomVertexEvent;
	import com.tongji.diy.event.furnitureOptimizationEvent.IterationCallbackEvent;
	import com.tongji.diy.event.furnitureOptimizationEvent.IterationPerFrameStartEvent;
	import com.tongji.diy.event.furnitureOptimizationEvent.IterationTestEvent;
	import com.tongji.diy.event.furnitureOptimizationEvent.OptimizationAddFurnitureEvent;
	import com.tongji.diy.event.furnitureOptimizationEvent.OptimizationShowCurrentLayoutEvent;
	import com.tongji.diy.event.ReturnDoorsInfoEvent;
	import com.tongji.diy.model.RoomDataModel;
	import com.tongji.diy.model.RoomDataVO;
	
	import flash.events.Event;
	import flash.geom.Point;
	
	import flare.basic.Scene3D;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class MainSceneMediator extends Mediator
	{
		[Inject]
		public var mainScene:MainScene;
		
		[Inject]
		public var roomDataModel:RoomDataModel;
		
		private var roomData:RoomDataVO;
		
		override public function onRegister():void
		{
			//test
			addContextListener(TestBoundBoxEvent.TEST_BOUND,testBound);
			
			
			
			addContextListener(NewDoorEvent.ADD_DOOR,addDoor);
			addContextListener(NewFurnitureEvent.ADD_FURNITURE, buildFurniture);
			addContextListener(BuildRoomEvent.BUILD_ROOM, buildRoom);
			addContextListener(SetRoomPositionEvent.SET_CURRENT_ROOM_POSITION, setCurrentRoomPosition);
			addContextListener(SetRoomRotationEvent.SET_CURRENT_ROOM_ROTATION, setCurrentRoomRotation);//frank
			addContextListener(UpdateRoomBorderEvent.UPDATE_ROOM_BORDER, updateRoomBorder);
			addContextListener(UpdateRoomVertexEvent.UPDATE_ROOM_VERTEX, updateRoomVertex);
			addContextListener(UpdateRoomFloorEvent.UPDATE_ROOM_FlOOR, updateRoomFloor);
			addContextListener(ShowNewShapeEvent.SHOW_NEW_SHAPE, showNewShape);
			addContextListener(ShowNewShapeEvent.SHOW_NEW_SHAPE_ARC, showNewShapeArc);
			addContextListener(AddHoleEvent.ADD_HOLE, addHole);
			addContextListener(ChangeShapeEvent.CHANGE_SHAPE_RECT, shapeToAddRect);
			addContextListener(ChangeShapeEvent.CHANGE_SHAPE_LWALL, shapeToAddLWall);
			addContextListener(ChangeShapeEvent.CHANGE_SHAPE_ARC, shapeToAddArc);
			addContextListener(FurnitureLocationAndRotation.SET_FURNITURE_LOCATION_AND_ROTATION, setFurnitureLocationAndRotation);
			addContextListener(FurnitureOverlapEvent.FURNITURE_OVERLAP, furnitureOverlap);
			//优化使用
			addContextListener(OptimizationShowCurrentLayoutEvent.SHOW_CURRENT_LAYOUT,showCurrentLayOut);
			addContextListener(Scene3D.UPDATE_EVENT, update);
			addContextListener(Scene3D.RENDER_EVENT, render);
			addContextListener(IterationPerFrameStartEvent.START_ITERATION, iterationStart);
			//zdp
			addContextListener(GetDoors.GETDOORS,getdoors);
			
			
			mainScene.addEventListener(AddRoomEvent.ADD_ROOM, addRoom);
			mainScene.addEventListener(AddNewFurnitureEvent.ADD_NEW_FURNITURE, dispatch);//frank
			mainScene.addEventListener(LockFurnitureEvent.LOCK_FURNITURE,dispatch);//frank
			mainScene.addEventListener(FurnitureFinalStateEvent.FINAL_POSITION, dispatch);//frank
			mainScene.addEventListener(UploadRoomPositionEvent.UPLOAD_ROOM_POSITION, dispatch);
			mainScene.addEventListener(UploadRoomRotationEvent.UPLOAD_ROOM_ROTATION, dispatch);
			mainScene.addEventListener(UploadRoomVertexEvent.UPLOAD_ROOM_VERTEX, dispatch);
			mainScene.addEventListener(UploadRoomBorderEvent.UPLOAD_ROOM_BORDER, dispatch);
			mainScene.addEventListener(RoomRotationEvent.ROOM_ROTATION_CLOCKWISE,dispatch);
			mainScene.addEventListener(RoomRotationEvent.ROOM_ROTATION_COUNTERCLOCKWISE,dispatch);
			mainScene.addEventListener(FurnitureManualRotationEvent.FURNITURE_ROTATION_CLOCKWISE,dispatch);
			mainScene.addEventListener(FurnitureManualRotationEvent.FURNITURE_ROTATION_COUNTERCLOCKWISE,dispatch);	
			
			mainScene.addEventListener(FurnitureRotationEvent.FURNITURE_ROTATION, dispatch);
			mainScene.addEventListener(AddShapeEvent.ADD_SHAPE, dispatch);
			mainScene.addEventListener(UploadHoleEvent.UPLOAD_HOLE, dispatch);
			
			//家具优化
			mainScene.addEventListener(OptimizationAddFurnitureEvent.OPTIMIZATION_ADD_FURNITURE, dispatch);
			mainScene.addEventListener(IterationCallbackEvent.CALL_BACK, dispatch);
			
			//zdp
			mainScene.addEventListener(ReturnDoorsInfoEvent.RETURNDOOSINFO,dispatch);
			
			//test
			mainScene.addEventListener(TestEnterClickEvent.ENTER_CLICK,dispatch);
			addContextListener(TestShowLayOutEvent.SHOW_LAYOUT, testShowLayout);
			
			
			mainScene.addEventListener(IterationTestEvent.TEST, test);
			addContextListener(IterationTestEvent.TEST, test);
			//mainScene.addEventListener(TestOptimizationResultShowCompleteFeedBackEvent.RESULT_SHOW_COMPLETE, dispatch);
			//mainScene.addEventListener(FurnitureOptimizationClickEvent.FURNITURE_OPTIMIZATION,furnitureOptimization);	
			
		}
		
		private function test(e:IterationTestEvent):void
		{
			mainScene.showCurrentLayout(e);
		}
		
		private function testShowLayout(e:TestShowLayOutEvent):void
		{
			mainScene.testShowLayout(e.furVector);
		}
		
		private function render(e:Event):void
		{
			mainScene.renderEvent(e);
		}
		
		private function update(e:Event):void
		{
			mainScene.updateEvent(e);
		}
		
		private function iterationStart(e:Event):void
		{
			mainScene.changeIterationSign();
		}
		
		private function showCurrentLayOut(e:OptimizationShowCurrentLayoutEvent):void
		{
			//mainScene.showCurrentLayout(e.optAccessibleBoundBoxes);
			//dispatch(new TestOptimizationResultShowCompleteFeedBackEvent(TestOptimizationResultShowCompleteFeedBackEvent.RESULT_SHOW_COMPLETE));
		}
		
		private function testBound(e:TestBoundBoxEvent):void
		{
			mainScene.testBoundBox(e);
		}
		
		private function addDoor(e:NewDoorEvent):void
		{
			mainScene.addDoor(e.walldoor);
		}
		
		private function furnitureOverlap(e:FurnitureOverlapEvent):void
		{
				mainScene.furnitureOverlap(e.overlap);
		}
		
		private function buildFurniture(e:NewFurnitureEvent):void
		{
			mainScene.buildFurniture(e.furniture.modSrc);
		}
		
		private function buildRoom(e:BuildRoomEvent):void
		{
			var roomShapePoints:Vector.<Point> = e.originalRoom.roomShapePoints;
			roomData =  new RoomDataVO(roomShapePoints,roomDataModel.roomIndex);
			mainScene.buildRoom(roomData);
		}
		
		private function addRoom(e:AddRoomEvent):void
		{
			roomDataModel.addRoom(roomData);
		}
		
		private function setFurnitureLocationAndRotation(e:FurnitureLocationAndRotation):void
		{
		
			mainScene.setFurnitureLocationAndAngle(e.location,e.angle);
		}
		
		private function setCurrentRoomPosition(e:SetRoomPositionEvent):void
		{
			mainScene.setCurrentRoomPosition(e.position);
		}
		
		private function setCurrentRoomRotation(e:SetRoomRotationEvent):void
		{
			mainScene.setCurrentRoomRotation(e.angle);	
		}
		
		private function updateRoomBorder(e:UpdateRoomBorderEvent):void
		{
			mainScene.updateRoomBorder(e.updateBorderArray);
		}
		
		private function updateRoomVertex(e:UpdateRoomVertexEvent):void
		{
			mainScene.updateRoomVertex(e.updateVertexArray);
		}
		
		private function updateRoomFloor(e:UpdateRoomFloorEvent):void
		{
			mainScene.updateRoomFloor(e.updateFloor);
		}
		
		private function showNewShape(e:ShowNewShapeEvent):void
		{
			mainScene.showNewShape(e.newPointArray,e.border,e.shapeType);
		}
		
		private function showNewShapeArc(e:ShowNewShapeEvent):void
		{
			mainScene.showNewShapeArc(e.newPointArray,e.border);
		}
		
		private function addHole(e:AddHoleEvent):void
		{
			mainScene.addHole(e.roomBorder,e.position);
		}
		
		private function shapeToAddRect(e:ChangeShapeEvent):void
		{
			mainScene.shapeToAddRect();
		}
		
		private function shapeToAddLWall(e:ChangeShapeEvent):void
		{
			mainScene.shapeToAddLWall();
		}
		
		private function shapeToAddArc(e:ChangeShapeEvent):void
		{
			mainScene.shapeToAddArc();
		}
		//zdp
		private function getdoors(e:GetDoors):void
		{
			mainScene.getdoors(e);
		}
		//////zdpend
	}	
}